﻿namespace PlugNT.Common.Http
{
    using System;
    using System.IO;
    using System.Web;

    public class PicHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            bool flag = true;
            context.Response.ContentType = "image/JPEG";
            if (context.Request.UrlReferrer != null)
            {
                if (context.Request.UrlReferrer.Host.ToLower() != context.Request.Url.Host.ToLower())
                {
                    flag = false;
                }
            }
            else
            {
                flag = false;
            }
            if (flag)
            {
                string fileName = context.Server.MapPath(context.Request.FilePath);
                if (File.Exists(fileName))
                {
                    context.Response.WriteFile(fileName);
                }
                else
                {
                    context.Response.WriteFile("/images/none.gif");
                }
            }
            else
            {
                context.Response.WriteFile("/images/logo.gif");
            }
        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    }
}

